﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace UI_Test
{
    public class File_Explorer 
    {

        public ListBox getlistBox( ListBox a)
        {
            return a;
        }


        public TreeView getTreview (TreeView v)
        {
            return v;
        }
        
        public void run(TreeView tree, ListBox h)
        {
            tree.AllowDrop = true;
            tree.CheckBoxes = true;
            tree.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView_AfterSelect);
        }

        private void Show(object sender, System.EventArgs e, TreeView a, ListBox b)
        {
            try
            {
                a.Nodes.Clear();
                b.Items.Clear();
                string[] drives = Environment.GetLogicalDrives();
                foreach (string drv in drives)
                {
                    TreeNode node = new TreeNode();
                    node.Text = drv;
                    a.Nodes.Add(node);
                    FillDirectory(drv, node, 0);
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        public void treeView_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            try
            {
                TreeNode node = e.Node;
                string strFullPath = node.FullPath;
                DisplayFiles(strFullPath, null);
                DirectoryInfo fi = new DirectoryInfo(strFullPath);
            }
            catch { }

        }

        private void DisplayFiles(string dirName, ListBox a)
        {
            try
            {
                a.Items.Clear();
                // test to see if directory exists
                DirectoryInfo dir = new DirectoryInfo(dirName);
                if (!dir.Exists)
                    throw new DirectoryNotFoundException
                        ("directory does not exist:" + dirName);

                // fill the directory listbox
                foreach (DirectoryInfo di in dir.GetDirectories())
                {
                    string str = "[Dir] " + di.Name;
                    a.Items.Add(str);
                }

                // fill the Files listbox
                foreach (FileInfo fi in dir.GetFiles())
                {
                   a.Items.Add(fi.Name);
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        private void FillDirectory(string drv, TreeNode parent, int level)
        {
            try
            {
                // I want to go only upto 3 level.
                level++;
                if (level > 4)
                    return;
                DirectoryInfo dir = new DirectoryInfo(drv);
                if (!dir.Exists)
                    throw new DirectoryNotFoundException
                        ("directory does not exist:" + drv);

                foreach (DirectoryInfo di in dir.GetDirectories())
                {
                    TreeNode child = new TreeNode();
                    child.Text = di.Name;
                    parent.Nodes.Add(child);

                    FillDirectory(child.FullPath, child, level);
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }

        }

    }
}
